package com.example.fbi_text.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.fbi_text.R;
import com.example.fbi_text.bean.News;

import java.util.List;

public class NewsAdapter extends BaseAdapter {
    private TextView titleText;
    private TextView authorText;
    private TextView TimeText;
    private List<News> mList;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    public NewsAdapter(List<News> list, Context context){
        mList = list;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void updateData(List<News> list) {
        mList.clear();
        this.mList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.main_item01, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.titleText = convertView.findViewById(R.id.main_item01_title);
            viewHolder.authorText = convertView.findViewById(R.id.main_item01_author);
            viewHolder.timeText = convertView.findViewById(R.id.main_item01_time);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        News news = mList.get(position);
        if (news != null) {
            // 设置数据到视图
            viewHolder.titleText.setText(news.getNews_title());
            viewHolder.authorText.setText(news.getNews_author());  // 修改：显示作者而非内容
            viewHolder.timeText.setText(news.getNews_time());

            // 将整个News对象绑定到视图的Tag
            convertView.setTag(R.integer.news_tag, news);
        }

        return convertView;
    }

    private static class ViewHolder {
        TextView titleText;
        TextView authorText;
        TextView timeText;
    }

}
